package com.demo.blog;

import com.demo.common.model.Blog;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * <p>
 * BlogValidator.
 */
public class BlogValidator extends Validator {

    @Override
    protected void validate(Controller controller) {
        validateRequiredString("blog.title", "titleMsg", "请输入Blog标题!");
        validateRequiredString("blog.content", "contentMsg", "请输入Blog内容!");
    }

    @Override
    protected void handleError(Controller controller) {
        controller.keepModel(Blog.class);

        String actionKey = getActionKey();
        if (actionKey.equals("/blog/save")) {
            controller.render("add.html");
        } else if (actionKey.equals("/blog/update")) {
            controller.render("edit.html");
        }
    }
}
